package org.iryshka.skyserver;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map.Entry;

public class HTTPResponse {
	
	private static HashMap<String, String> statusToMessageMap = new HashMap<String, String>();
	private static HashMap<String, String> statusToExtMessageMap = new HashMap<String, String>();
	
	static {
		statusToMessageMap.put("200", "OK");
		addStatusMapData("400", "Bad Request", "The request cannot be fulfilled due to bad syntax");
		addStatusMapData("403", "Forbidden", "The request cannot be fulfilled by server");
		addStatusMapData("404", "Not Found",
		        "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable");
		addStatusMapData("405", "Method Not Allowed",
		        "A request was made of a resource using a request method not supported by that resource");
		addStatusMapData("429", "Too Many Requests", "Too many requests in a given amount of time.");
	}
	
	private static void addStatusMapData(final String statusCode, final String message, final String extMessage) {
		statusToMessageMap.put(statusCode, message);
		statusToExtMessageMap.put(statusCode, extMessage);
	}
	
	public static String getMessageFromStatus(final String status) {
		return statusToMessageMap.get(status);
	}
	
	public static String getExtMessageFromStatus(final String status) {
		return statusToExtMessageMap.get(status);
	}
	
	public void setHeaderInMap(final String header, final String value) {
		mapRespHeaders.put(header, value);
	}
	
	public HashMap<String, String> mapRespHeaders = new HashMap<String, String>();
	private String statusCode = "200";
	private String statusString;
	private InputStream resourceStream;
	
	public String getStatus() {
		return statusCode;
	}
	
	public void setStatus(final String newStatus) {
		statusCode = newStatus;
	}
	
	public String getStatusString() {
		return statusString;
	}
	
	public void setStatusString(final String newStatus) {
		this.statusString = newStatus;
	}
	
	public InputStream getResourceStream() {
		return resourceStream;
	}
	
	public void setResourceStream(final InputStream resourceStream) {
		this.resourceStream = resourceStream;
	}
	
	public String toString() {
		String helpStr = "HTTP/1.1 " + statusCode + " " + getMessageFromStatus(statusCode) + "\r\n";
		for (final Entry<String, String> mapEntry : mapRespHeaders.entrySet()) {
			helpStr = helpStr + mapEntry.getKey() + mapEntry.getValue() + "\r\n";
		}
		return helpStr + "\r\n";
	}
}
